package com.ruoyi.web.controller.base;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseBomInfo;
import com.ruoyi.system.domain.base.BaseMaterial;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.mapper.BaseMaterialMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseBomInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * bom详情Controller
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Api(tags = "bom详情api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/info" )
public class BaseBomInfoController extends BaseController {

    private final IBaseBomInfoService iBaseBomInfoService;

    /**
     * 查询bom详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseBomInfo baseBomInfo)
    {
        startPage();
        LambdaQueryWrapper<BaseBomInfo> lqw = new LambdaQueryWrapper<BaseBomInfo>();
        if (StringUtils.isNotBlank(baseBomInfo.getPhotoNum())){
            lqw.eq(BaseBomInfo::getPhotoNum ,baseBomInfo.getPhotoNum());
        }
        if (StringUtils.isNotBlank(baseBomInfo.getMaterialName())){
            lqw.like(BaseBomInfo::getMaterialName ,baseBomInfo.getMaterialName());
        }
        if (StringUtils.isNotBlank(baseBomInfo.getStuff())){
            lqw.eq(BaseBomInfo::getStuff ,baseBomInfo.getStuff());
        }
        if (StringUtils.isNotBlank(baseBomInfo.getVersionNum())){
            lqw.eq(BaseBomInfo::getVersionNum ,baseBomInfo.getVersionNum());
        }
        if (baseBomInfo.getType() != null){
            lqw.eq(BaseBomInfo::getType ,baseBomInfo.getType());
        }
        List<BaseBomInfo> list = iBaseBomInfoService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出bom详情列表
     */
    @Log(title = "bom详情" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseBomInfo baseBomInfo) {
        LambdaQueryWrapper<BaseBomInfo> lqw = new LambdaQueryWrapper<BaseBomInfo>(baseBomInfo);
        List<BaseBomInfo> list = iBaseBomInfoService.list(lqw);
        ExcelUtil<BaseBomInfo> util = new ExcelUtil<BaseBomInfo>(BaseBomInfo. class);
        return util.exportExcel(list, "info" );
    }

    /**
     * 获取bom详情详细信息
     */
    @ApiOperation(value = "获取bom详情详细信息")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBaseBomInfoService.getById(id));
    }

    @Autowired
    private BaseMaterialMapper baseMaterialMapper;

    /**
     * 新增bom详情
     */
    @ApiOperation(value = "新增bom详情")
    @Log(title = "bom详情" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseBomInfo baseBomInfo) {
        //如果新增的不是物料类型,物料编码应该为空
        if(baseBomInfo.getType().intValue()==4){
            if(StrUtil.isNotEmpty(baseBomInfo.getMaterialCode())){
                throw new CustomException("此类型不存在物料编码");
            }
        }
        //根据物料编码获取物料名称
        BaseMaterial baseMaterial = baseMaterialMapper.selectOne(new QueryWrapper<BaseMaterial>().eq("material_code", baseBomInfo.getMaterialCode()));
        baseBomInfo.setMaterialName(baseMaterial.getMaterialName());


        if (UserConstants.NOT_UNIQUE.equals(iBaseBomInfoService.checkBomNameUnique(baseBomInfo)))
        {
            return AjaxResult.error("新增BOM'" + baseBomInfo.getMaterialName() + "'失败，BOM名称已存在");
        }
        baseBomInfo.setCreateBy(SecurityUtils.getUsername());
        baseBomInfo.setCreateTime(new Date());
        return toAjax(iBaseBomInfoService.save(baseBomInfo)? 1 : 0);

    }

    /**
     * 修改bom详情
     */
    @ApiOperation(value = "修改bom详情")
    @Log(title = "bom详情" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseBomInfo baseBomInfo) {
        //如果修改的不是物料类型,物料编码应该为空
        if(baseBomInfo.getType().intValue()==4){
            if(StrUtil.isNotEmpty(baseBomInfo.getMaterialCode())){
                throw new CustomException("此类型不存在物料编码");
            }
        }
        return toAjax(iBaseBomInfoService.updateById(baseBomInfo) ? 1 : 0);
    }

    /**
     * 删除bom详情
     */
    @ApiOperation(value = "删除bom详情")
    @Log(title = "bom详情" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (iBaseBomInfoService.hasChildByDeptId(ids[0]))
        {
            return AjaxResult.error("存在下级,不允许删除");
        }
        return toAjax(iBaseBomInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 获取bom详情下拉树列表
     */
    @ApiOperation(value = "获取bom详情下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BaseBomInfo baseBomInfo)
    {
        List<BaseBomInfo> infos = iBaseBomInfoService.selectBomInfoList(baseBomInfo);
        return AjaxResult.success(iBaseBomInfoService.buildBomInfoTreeSelect(infos));
    }

}
